<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<c:set var="ctx" value="${pageContext.request.contextPath}"/>


<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<head>
<meta charset="utf-8" />
<title>青麦门店系统 |${title }</title>
<meta
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
	name="viewport" />
<meta content="" name="description" />
<meta content="" name="author" />

<!-- ================== BEGIN BASE CSS STYLE ================== -->
<%@ include file="../include/inc_head.jsp"%>
<!-- ================== END BASE CSS STYLE ================== -->

<!-- ================== BEGIN BASE JS ================== -->
<script src="/assets/plugins/pace/pace.min.js"></script>
<link rel="stylesheet" type="text/css" href="/assets/css/orderStatus.css"> 
<!-- ================== END BASE JS ================== -->
</head>
<style>
.form-horizontal.form-bordered .form-group>.control-label {
    padding: 20px 158px 15px;
    border-right: 1px solid #eee;
    margin-right: -1px;
}
.form-horizontal.form-bordered .form-group>div {
    padding: 20px;
    border-left: 1px solid #eee;
}

.table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td, .table>tfoot>tr>th, .table>thead>tr>td, .table>thead>tr>th {
    padding: 8px;
    line-height: 1.42857143;
    vertical-align: middle; 
    border-top: 1px solid #ddd;
}
.datagrid-view{
height: 500px;
}
</style>
<body>
	<!-- begin #page-loader -->
	<div id="page-loader" class="fade in">
		<span class="spinner"></span>
	</div>
	<!-- end #page-loader -->

	<!-- begin #page-container -->
	<div id="page-container"
		class="fade page-sidebar-fixed page-header-fixed">
		<!-- begin #header -->
		<%@ include file="../include/inc_header.jsp"%>
		<!-- end #header -->

		<!-- begin #sidebar -->
		<%@ include file="../include/inc_left.jsp"%>
		<!-- end #sidebar -->

		<!-- begin #content -->
		<div id="content" class="content">
			<!-- begin breadcrumb -->
			<ol class="breadcrumb pull-right">
				<li><a href="javascript:;">首页</a></li>
				<li class="active">${title }</li>
			</ol>
			<!-- end breadcrumb -->
			<!-- begin page-header -->
			<h1 class="page-header">${title }</h1>
				<button type="button" class="btn btn-primary m-r-5 m-b-5" onclick="back();">返回</button>
			<!-- end page-header -->
			   
			
 			<%-- 	 <div class="table-responsive">
                        <table id="user" class="table table-bordered table-striped">
                            <thead>
                             <tr>
                                    <td style="width:220px;font-weight:900;font-size: 16px;">入库单信息</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td style="width:320px;">采购单ID:<a href="#"  style="margin-left: 30px;">${procurementOrderId}</a></td>
                                </tr>
                                <tr>
                                    <td style="width:320px;">商家:<a href="#" style="margin-left: 30px;">${shopName }</a></td>
                                </tr>
                                 <tr>
                                    <td style="width:320px;">入库时间:<a href="#" style="margin-left: 30px;" >${instockTime }</a></td>
                                </tr>  
                            </tbody>
                        </table>                          
                    </div> --%>
                      <div class="panel panel-inverse" width="100%" data-sortable-id="table-basic-1">
                        <div class="panel-heading" style="width:100%;background: #a5bcd2;">
                            <h4 class="panel-title">入库单商品</h4>
                        </div>                        
                        <table id="dg" class="easyui-datagrid" title="" style="width:auto;height:auto"
				            data-options="iconCls: 'icon-edit',
				                singleSelect: true,
				                toolbar: '#tb',
				                url: '${ctx}/shop/stock/getItem?id=${id }',
				                method: 'get'">
				        <thead>         
				            <tr> 
				            <th data-options="field:'id',width:80,align:'center'">入库单行ID</th>
				                <th data-options="field:'procurementOrderItemId',width:200,align:'center'">采购单行ID</th>
				                <th data-options="field:'skuName',width:200,align:'center'">商品名称</th>
				                 <th data-options="field:'skuFormat',width:200,align:'center'">商品规格</th>
				                <th data-options="field:'num',width:100,align:'center',editor:'text'">入库数量</th>
				                <th data-options="field:'weight',width:100,align:'center',editor:'text'">入库重量(KG)</th>
				                <th data-options="field:'time',width:300,align:'center'">入库时间</th> 
				            </tr>
				        </thead>
				    </table>
 
					    <div id="tb" style="height:auto">
					        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="getNeedInstock()">新增</a>
					        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="edit()">编辑</a>
					    <!--     <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>  -->
					       <!--  <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a> -->
					   <!--      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">GetChanges</a> -->
					    </div>
                    
                    </div>
                    
                        <div style="margin:20px 0;">
      <!--   <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#hover'+'treewindow').window('open')">Open</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#hovertreewindow').window('close')">Close</a> -->
    </div>
    <div id="hovertreewindow" class="easyui-window" title="修改" data-options="iconCls:'icon-save'" style="width:500px;height:200px;padding:10px;">
	   
	  		 <div style="margin-top: 20px;"> 	
	   			<span>入库数量：</span><input type="text" id="num"  placeholder="" />
	    		<span>入库重量：</span><input type="text" id="weight"  placeholder="" />
	    	</div>
	    	<div style="margin-top: 30px;text-align: center">
					<a href="javascript:;"   class="btn btn-primary btn-sm m-r-5"  onclick="saveEdit();"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">保存</font></font></a>
					<a href="javascript:;"   class="btn btn-primary btn-sm m-r-5" onclick="$('#hovertreewindow').window('close')"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">取消</font></font></a>
	    	</div>
	 </div>
	 
	     <div id="hovertreewindow2" class="easyui-window" title="修改" data-options="iconCls:'icon-save'" style="width:500px;padding:10px;">
	   
	  		 <div style="margin-top: 20px;"> 	
	   			<span>入库重量(kg)：</span><input type="text" id="num2"  placeholder="" />
	    	</div>
	    	<div style="margin-top: 20px;"> 	
	   			<span>入库数量：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="weight2"  placeholder="" />
	    	</div>
	    	<div style="margin-top: 20px;"> 	
	   			<span>采购单ID：</span><select id="caigou" style="width: 100px" onchange="changeSelect();"></select>
	    	</div>
	    	<div style="margin-top: 20px;"> 	
	   			<span>选择可入库的商品：</span><select id="goods" style="width: 100px" onchange="changeGoods();"></select>
	    	</div>
	    	<div style="margin-top: 30px;text-align: center">
					<a href="javascript:;"   class="btn btn-primary btn-sm m-r-5"  onclick="saveItem();"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">保存</font></font></a>
					<a href="javascript:;"   class="btn btn-primary btn-sm m-r-5" onclick="$('#hovertreewindow2').window('close')"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">取消</font></font></a>
	    	</div>
	 </div>
		</div>

		<!-- end #content -->

		<%@ include file="../include/inc_footer.jsp"%>
		<!-- begin scroll to top btn -->
		<a href="javascript:;"
			class="btn btn-icon btn-circle btn-success btn-scroll-to-top fade"
			data-click="scroll-top"><i class="fa fa-angle-up"></i></a>
		<!-- end scroll to top btn -->
	</div>
	<!-- end page container -->

	<!-- ================== BEGIN BASE JS ================== -->
	<%@ include file="../include/inc_foot.jsp"%>

	<!-- ================== END BASE JS ================== -->
	<!-- ================== BEGIN PAGE LEVEL JS ================== -->
	<script src="/assets/plugins/DataTables/media/js/jquery.dataTables.js"></script>
	<script src="/assets/plugins/DataTables/media/js/dataTables.bootstrap.min.js"></script>
	<script src="/assets/plugins/DataTables/extensions/Responsive/js/dataTables.responsive.min.js"></script>
	<script src="/assets/js/table-manage-default.demo.min.js"></script>
	<script src="/assets/js/apps.min.js"></script>
	<!-- ================== END PAGE LEVEL JS ================== -->

<link rel="stylesheet" type="text/css" href="/assets/jQueryEasyui/themes/gray/easyui.css">  
<link rel="stylesheet" type="text/css" href="/assets/jQueryEasyui/themes/icon.css">  

<script type="text/javascript" src="/assets/jQueryEasyui/jquery.easyui.min.js"></script> 
<script type="text/javascript" src="/assets/jQueryEasyui/easyui-lang-zh_CN.js"></script> 

	<script>
		$(document).ready(function() {
		$('#hovertreewindow').window('close');
		$('#hovertreewindow2').window('close');
			App.init();
			TableManageDefault.init();
		});
		
		function back(){
			history.go(-1);
		}
		
	</script>

<script type="text/javascript">
        var editIndex = undefined;
        function endEditing(){
        	console.log('s1');
            if (editIndex == undefined){return true}
            if ($('#dg').datagrid('validateRow', editIndex)){
                var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'classId'});
            
                var productname = $(ed.target).combobox('getText');
                $('#dg').datagrid('getRows')[editIndex]['name'] = productname;
                $('#dg').datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }
        function onClickRow(index){
        	console.log('s2');
            if (editIndex != index){
                if (endEditing()){
                    $('#dg').datagrid('selectRow', index)
                            .datagrid('beginEdit', index);
                    editIndex = index;
                } else {
                    $('#dg').datagrid('selectRow', editIndex);
                }
            }
        }
        function append(){
        	console.log('s3');
            if (endEditing()){
                $('#dg').datagrid('appendRow',{status:'P'});
                editIndex = $('#dg').datagrid('getRows').length-1;
                $('#dg').datagrid('selectRow', editIndex)
                        .datagrid('beginEdit', editIndex);
            }
        }
        function removeit(){
        	console.log('s4');
            if (editIndex == undefined){return}
            $('#dg').datagrid('cancelEdit', editIndex)
                    .datagrid('deleteRow', editIndex);
            editIndex = undefined;
            var rows = $('#dg').datagrid('getChanges');
             alert(rows.length+' rows are changed!');
              if (endEditing()){
                $('#dg').datagrid('acceptChanges');
            }
        }
        function accept(){
        	console.log('s5');
        	getChanges();
            if (endEditing()){
                $('#dg').datagrid('acceptChanges');
            }
            
        }
        function reject(){
            $('#dg').datagrid('rejectChanges');
            editIndex = undefined;
        }
        function getChanges(){
        	console.log('s6');
            var rows = $('#dg').datagrid('getChanges');
            alert(rows.length+' rows are changed!');
        }
        
        function edit(){
	         var rows = $('#dg').datagrid("getSelections");
		      if (rows.length == 0) {
		        $.messager.alert('提示', '请选择要编辑的商品!');
		        return;
		      }
		      if (rows.length > 1) {
		    	  $.messager.alert('提示', '只能选择商品进行编辑!');
		        return;
		      }
		      $('#num').val(rows[0].num);
		      $('#weight').val(rows[0].weight);
	        $('#hover'+'treewindow').window('open');
        }
        
        function saveEdit(){
        var id= $('#dg').datagrid("getSelections")[0].id;
        var num= $('#num').val();
        var weight= $('#weight').val();
        var reg = new RegExp("^[0-9]*$");
	    if(!reg.test(num)){
	        alert("请输入数字!");
	        return;
	    }
	     if(!reg.test(weight)){
	        alert("请输入数字!");
	        return;
	    }
         $.ajax({
             type: "post",
             url: "/shop/stock/changeItem",
             data: {
             id:id,
             weight:weight,
             num:num
             },
             dataType: "json",
             success: function(data){
               if(data==true){
               $('#hovertreewindow').window('close');
               doSearch();
               }else{
                $.messager.show({    // show error message
	                                    title: '提示',
	                                    msg: '修改失败'
	                                });
               }       
                       
             }
         });
        }
        
    function doSearch() {
        $('#dg').datagrid('load', {
        });
    }
    
    
      var saveNum=[];
      var saveWeight=[];
      function getNeedInstock(index){
      var id='${procurementOrderId}';
      if(index!=null){
      	  id=index;	
      }
      if(id==''&&index==undefined){
      getNeedInstockOrder();
      }else{
          $.ajax({
             type: "post",
             url: "/shop/stock/getNeedInstock",
             data: {
             procurementId:id
             },
             dataType: "json",
             success: function(data){
              if(data!=null&&data.length>0&&data[0]!=null){
              var str='';
              for(var i=0;i<data.length;i++){
              str=str+'<option id="'+data[i].id+'#'+data[i].skuId+'">'+data[i].skuName+'</option>';
              saveNum[data[i].id+'#'+data[i].skuId]=data[i].expectNum;
              saveWeight[data[i].id+'#'+data[i].skuId]=data[i].expectWeight;
              }
              if(index!=undefined){
              str='<option id="'+data[0].id+'#'+data[0].skuId+'">'+data[0].skuName+'</option>';
              
              }
              $('#goods').html(str);
               $('#num2').val(data[0].expectNum);
        		$('#weight2').val(data[0].expectWeight);
        		if($('#caigou').html()==""){
        		$('#caigou').html('<option>'+id+'</option>');
        		}
               $('#hover'+'treewindow2').window('open');
              }else{
              $.messager.show({    // show error message
	                                    title: '提示',
	                                    msg: '没有可入库的商品'
	                                });
              }
                       
             }
         });
      }
      
    
        }
        
        
          function getNeedInstockOrder(){
        $.ajax({
             type: "post",
             url: "/shop/stock/getNeedInstock",
             data: {
             procurementId:-1
             },
             dataType: "json",
             success: function(data){
             if(data!=null&&data.length>0){
              var procurementId='';
              var str='';
                 for(var i=0;i<data.length;i++){
                 if(procurementId.indexOf(data[i].procurementOrderId+'#') < 0){
                 str=str+'<option>'+data[i].procurementOrderId+'</option>';
                 }
                 procurementId=procurementId+data[i].procurementOrderId+'#';
                 }
                 $('#caigou').html(str);
                 getNeedInstock(data[0].procurementOrderId);
             }else{
               $.messager.show({    // show error message
               							showtype:null,
               							style:{},
	                                    title: '提示',
	                                    timeout:1500,
	                                    msg: '没有可入库的商品'
	                                });
             }
             }
         });
        }
        
        function saveItem(){
        var procurementId='${procurementOrderId}';
        if(procurementId==''){
        procurementId=$('#caigou option:selected').html();
        }
        var num=$('#num2').val();
        var weight=$('#weight2').val();
         var reg = new RegExp("^[0-9]*$");
	    if(!reg.test(num)||num==''){
	         $.messager.show({    // show error message
	                                    title: '提示',
	                                    msg: '请输入数字!'
	                                });
	        return;
	    }
	     if(!reg.test(weight)||weight==''){
	         $.messager.show({    // show error message
	                                    title: '提示',
	                                    msg: '请输入数字!'
	                                });
	        return;
	    }
        var procurementOrderItemId=$('#goods option:selected').attr("id").split('#')[0];
           $.ajax({
             type: "post",
             url: "/shop/stock/createStock",
             data: {
             procurementId:procurementId,
             num:num,
             weight:weight,
             procurementOrderItemId:procurementOrderItemId
             },
             dataType: "json",
             success: function(data){
              if(data==true){
               $('#hovertreewindow2').window('close');
               doSearch();
               }else{
                $.messager.show({    // show error message
	                                    title: '提示',
	                                    msg: '修改失败'
	                                });
               } 
                       
             }
         });
        }
        
        function changeSelect(){
        var procurementId=$('#caigou option:selected').html();
        getNeedInstock(procurementId);
        }
        
        function changeGoods(){
        var id=$('#goods option:selected').attr('id');
        $('#num2').val(saveNum[id]);
        $('#weight2').val(saveWeight[id]);
        
        }
    </script>
</body>
</html>


